package net.cooltain.warhammer.running.entity;

import java.util.ArrayList;
import java.util.List;

import net.cooltain.warhammer.framework.constant.ConstantCardType;

/**
 * 包括战场区\王国区\任务区\首都区
 * @author cooltian
 *
 */
public abstract class AreaZoneEntity extends ZoneEntity{
    
    public AreaZoneEntity(MotionPlayerZone player) {
        super(player);
    }

    protected static int defenceInitNum= 8; // 初始防御值
    protected static int damageInitNum = 0; // 初始伤害值
    
    protected int defenceCurrent= defenceInitNum; // 当前的防御值,不含设施卡
    protected int damageCurrent = damageInitNum;  // 当前的伤害标记
    protected boolean isBufning = false;  // 是否焚城
    
    protected boolean beAttacked = false; // 是否被对手进攻

    // =========================================================================
    // ========================== Get / Set ====================================
    // =========================================================================
    
    
    /**
     * 获得当前区域是否焚城
     * @return
     */
    public boolean isBufning() {
        return isBufning;
    }

    /**
     * 获得当前的总防御值<br>
     * 总防御值=当前防御+设施数
     * @return
     */
    public int getDefenceSum(){
        return defenceCurrent + getDevelopmentCount();
    }
    
    /**
     * 获得当前总伤害标记数<br>
     * @return
     */
    public int getDamageSum(){
        return damageCurrent;
    }

    /**
     * 获得剩余防御值<br>
     * 剩余防御值=当防御+设施数-伤害标记
     * @return
     */
    public int getDefenceLevel(){
        return defenceCurrent + getDevelopmentCount() - damageCurrent;
    }
    
    /**
     * 获取所有的设施卡列表
     * @return
     */
    public List<MotionCard> getDevelopmentCards(){
        List<MotionCard> development = new ArrayList<MotionCard>();
        for(MotionCard card : this.cards){
            if(card.isDevelopment()){
                development.add(card);              
            }
        }
        return development;
    }
    
    /**
     * 获取当前区域设施卡的总数
     * @return
     */
    public int getDevelopmentCount(){
        int count = 0;
        for(MotionCard card : this.cards){
            if(card.isDevelopment()){
                count++;        
            }
        }
        return count;
    }
    

	/**
	 * 获得当前生命值与最大生命值,以及未计算的伤害
	 * @return
	 */
	public String getLabelHealthAndDefend(){
		if(this.isBufning()){
			return "焚城";
		}else{
			return new StringBuffer()
							.append(getDefenceLevel())
							.append("/")
							.append(getDefenceSum()).toString();
		}
	}
	
	/**
	 * 获得当前区域的指定种族旗帜总数
	 * @param raceType 指定种族
	 * @return
	 */
	public int getRaceTypeCount(Integer raceType){
		int count = 0;
		for(MotionCard card : this.cards){
			// 不是设施卡,不是背面朝上,是指定的种族类型
			if( !card.isDevelopment()
			    && !card.isFlip() 
			    && card.cardEntity.getRace()==raceType){
				count ++;
			}
		}
		return count;
	}
	
	/**
	 * 是否被对手选为攻击区域
	 * @return
	 */
	public boolean isBeAttacked() {
		return beAttacked;
	}
	
	/**
	 * 被对手设为攻击区域
	 * @param beAttacked
	 */
	public void setBeAttacked(boolean beAttacked) {
		this.beAttacked = beAttacked;
	}
	

    
    // =========================================================================
    // ========================== 行为   =========================================
    // =========================================================================

	public void sortCard(){
    	List<MotionCard> unitList = new ArrayList<MotionCard>();
    	List<MotionCard> supportList = new ArrayList<MotionCard>();
    	List<MotionCard> develepList = new ArrayList<MotionCard>();
    	List<MotionCard> attachmentList = new ArrayList<MotionCard>();
    	List<MotionCard> otherList = new ArrayList<MotionCard>();
    	for(MotionCard card :this.cards){
    		//this.cards.remove(i);
    		if(card.isDevelopment()){
    			develepList.add(card);
    		}else if(card.isAttachment()){
    			attachmentList.add(card);
    		}else if(card.cardEntity.getType()==ConstantCardType.CARD_TYPE_UNIT){
    			unitList.add(card);
    		}else if(card.cardEntity.getType()==ConstantCardType.CARD_TYPE_UNIT){
    			supportList.add(card);
    		}else {
    			otherList.add(card);
    		}
    	}
    	unitList.addAll(supportList);
    	unitList.addAll(otherList);
    	unitList.addAll(develepList);
    	unitList.addAll(attachmentList);
    	this.cards = unitList;
    }

    
	
    
    // =========================================================================
    // ========================== 事件   =========================================
    // =========================================================================
    
    /**
     * 事件:游戏区增加一张牌
     */
	@Override
	public void eventAddCard(MotionCard card) {
		super.eventAddCard(card);
		
		// 自己可以看到自己的游戏区的所有卡片
		card.addVisiblePlayer(player);
		if(!card.isDevelopment()){
			// 非设施卡,对方玩家默认可见
			card.addVisiblePlayer(player.operatePlayer);			
		}
	}
	
	/**
	 * 事件:游戏区增加伤害
	 * @param dmgNumber
	 * @throws InterruptedException 
	 */
	public void eventAddDamage(int dmgNumber) throws InterruptedException{
		if( !this.isBufning()){
			this.damageCurrent += dmgNumber ;
			if(this.getDefenceLevel()<=0){
				this.isBufning = true;
			}
		}
	}
	
	/**
	 * 事件:游戏区减少伤害
	 * @param dmgNumber
	 */
	public void eventRemoveDamage(int dmgNumber){
		if( !this.isBufning()){
			this.damageCurrent -= dmgNumber ;
			if(this.damageCurrent<0){
				this.damageCurrent = 0;
			}
		}
	}
}
